Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

647
Views
MongoDB database. Error "Select a valid choice. That choice is not one of the available choices."

I have installed MongoDB in my Django project. Because is the first time I use Mongo, I decided to try how it works and I created a simple program to store data ( price and quantity in my case). The project is called "exchange" and it has 2 folders: exchange and app.

This is the file models.py from 'app' folder:

from django.db import models
from djongo.models.fields import ObjectIdField, Field
from django.contrib.auth.models import User

class Profile(models.Model):
    _id = ObjectIdField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)

class Order(models.Model):
    _id = ObjectIdField()
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    datetime = models.DateTimeField(auto_now_add=True)
    price = models.FloatField()
    quantity = models.FloatField()
    #ips = models.Field(default=[])
    #subprofiles = models.Field(default={})

This is the file admin.py

from django.contrib import admin
from .models import *

admin.site.register(Profile)
admin.site.register(Order)

This is how I set the database in the file settings.py of the exchange folder

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'engine',
    }
}

So after made the migrations and create the superuser, I run the server, I went into the section Admin than in Profile, and I created a profile choosing the only option available (my superuser). At this point I created an order in the Orders section: so I chose in the "profile" field the only option available (the profile created before), then I filled the other 2 fields (price and quantity), but when I try to save it, appears above the field "profile" the following error:

"Select a valid choice. That choice is not one of the available choices."

I cannot understand where I am wrong.

Thanks in advance for your help!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The issue is that ForeignKey by default appends _id to the property name. In your above example, it is trying to use the property user_id on Profile and profile_id on Order.

The solution is to update the ForeignKey to:

profile = models.ForeignKey(Profile, db_column='profile', on_delete=models.CASCADE)

What you change db_column to may depend on how you have your schema set up inside mongodb, but I was receiving this same error and point the db_column to the correct property fixed it for me.

over 4 years ago · Santiago Trujillo Report

0

OMG @Adam Berg , I have been trying to resolve this issue for more than past 4 hours .Your answer resolved it within in mints .Thank you so much , your work is much appreciable.

import datetime,os
from djongo import models

class test1(models.Model):
   name = models.CharField(max_length=100)


class Profile(models.Model):
   user = models.ForeignKey(test1, db_column='user',on_delete=models.CASCADE)
     

This Will help you to perform all the foreign key related Query.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!